﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using EcsSiteDSTableAdapters;


namespace SiteECSWeb
{
    [DataObject]
    public class WcompanyBLL
    {
        private WcompanyTableAdapter _WcompanyAdapter = null;

        protected WcompanyTableAdapter WcompanyAdapter
        {
            get
            {
                if (_WcompanyAdapter == null)
                {
                    _WcompanyAdapter = new WcompanyTableAdapter();
                }
                return _WcompanyAdapter;
            }
        }


        #region SelectWcompany

        // get all Wcompany Data//

        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public EcsSiteDS.WcompanyDataTable GetAllWcomapny()
        {
            return WcompanyAdapter.GetWcompanyRowRankData();
        }
        
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.WcompanyDataTable GetWcompanyByGridPagesFilerExp(int startRowIndex,
            int maximumRows, string sortExpression)
        {
            return WcompanyAdapter.GetWcompanyDataByGridPages(startRowIndex, maximumRows, sortExpression);
            //    EcsSiteDS.ExpOpprtintyDataTable ExpOppr = GetAllExportOpp();
            //    PagedDataSource pagedData = new PagedDataSource();
            //    pagedData.DataSource = ExpOppr.Rows;
            //    pagedData.AllowPaging = true;
            //    pagedData.CurrentPageIndex = pageIndex;
            //    pagedData.PageSize = pageSize;
            //    return pagedData;
        }


        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.WcompanyDataTable GetWcompanyByGridPagesFilerExp(int startRowIndex, int maximumRows,
            string FilterStr, string sortExpression)
        {
            if (string.IsNullOrEmpty(FilterStr))
            {
                return
                    GetWcompanyByGridPagesFilerExp(startRowIndex, maximumRows, sortExpression);
            }
            else
            {
                return WcompanyAdapter.GetWcompanyDataByGridPagesFilerExp
                    (startRowIndex, maximumRows, FilterStr, sortExpression);
            }
        }


        // Get Wcompany Count
        public int GetTotalNumberOfWcompany()
        {
            Nullable<int> totalNull = (Nullable<int>)WcompanyAdapter.TotalNumberOfWCompany();
            return totalNull.GetValueOrDefault();
        }
        public int GetTotalNumberOfWcompany(string FilterStr)
        {
            if (string.IsNullOrEmpty(FilterStr))
            {
                return GetTotalNumberOfWcompany();
            }
            else
            {
                Nullable<int> totalNull = (Nullable<int>)WcompanyAdapter.
                TotalNumberOfWcompanyByFilterExp(FilterStr);
                return totalNull.GetValueOrDefault();
            }
        }

        // get all WCompany Data by CuntryNmEn//


        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.WcompanyDataTable GetWcompanyDataByCuntryNmEn(string CuntryNmEn)
        {
            return WcompanyAdapter.GetWcompanyDataByCuntryNmEn(CuntryNmEn);
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.WcompanyDataTable 
            GetWcompanyByFullNameEnFrstLetter
            (string FullNameEn, int TopCount)
        {
            return WcompanyAdapter.GetWcompanyDataByWcompNmEnFrstLetter(FullNameEn,TopCount);
        }

        // get all WCompany Data by CityID//
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]

        public EcsSiteDS.WcompanyDataTable GetWcompanybyCityid(int city_id)
        {
            if (city_id <= 0)
            {
                return WcompanyAdapter.GetWcompanyRowRankData();
            }
            else
            {
                return WcompanyAdapter.GetWcompanyDataByCityId(city_id);
            }
        }

        // Get Count Wcompany By City //
        //[DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        //public EcsSiteDS.WcompanyDataTable GetCountWcompByCityId(int City_id)
        //{
        //    return WcompanyAdapter.GetCountWcompanyDataByCity(City_id);
        //}

        //Get All Wcompany by email//
        //[DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        //public EcsSiteDS.WcompanyDataTable GetWcompanyByEmail1OrEmail2(string Wcomp_email1)
        //{
        //    return WcompanyAdapter.GetWcamopanyDataByEmail1(Wcomp_email1);
        //}

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.WcompanyDataTable GetWcompanyDataByWCompanyID(int WCompanyID)
        {
            return WcompanyAdapter.GetWcompanyDataByWcompId(WCompanyID);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.WcompanyDataTable GetWcompanyDataByWcompNmEnFrstLetter(string Wcomp_fulnmen ,int TopCount)
        {
            return WcompanyAdapter.
                GetWcompanyDataByWcompNmEnFrstLetter(Wcomp_fulnmen , TopCount);
        }

        #endregion


        #region InsertWcompany

        [DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
        public int AddWcompany(string Wcomp_fulnmar,
            string Wcomp_smlnmar, string Wcomp_fulnmen,
            string Wcomp_smlnmen, string Wcomp_addrs, string Wcomp_desc,
            string Wcomp_phn1, string Wcomp_phn2, string Wcomp_fax1,
            string Wcomp_fax2, string Wcomp_mbil1, string Wcomp_mbil2,
            string Wcomp_email1, string Wcomp_email2, string Wcomp_wsite,
            bool? Wcomp_improtfrmEG, int? CompType_Id,
            int City_id, int Ecs_InternalOffice_ID, bool? Formemberonly, 
            int? WComp_Sector_id, string WComp_ProductRangeAr, string WComp_ProductRangeEn)
        {

            EcsSiteDS.WcompanyDataTable WcompTable = new EcsSiteDS.WcompanyDataTable();
            EcsSiteDS.WcompanyRow WcompRow = WcompTable.NewWcompanyRow();

            if (string.IsNullOrEmpty(Wcomp_fulnmar)) WcompRow.SetWcomp_fulnmarNull(); else WcompRow.Wcomp_fulnmar = Wcomp_fulnmar;
            if (string.IsNullOrEmpty(Wcomp_smlnmar)) WcompRow.SetWcomp_smlnmarNull(); else WcompRow.Wcomp_smlnmar = Wcomp_smlnmar;
            if (string.IsNullOrEmpty(Wcomp_addrs)) WcompRow.SetWcomp_addrsNull(); else WcompRow.Wcomp_addrs = Wcomp_addrs;
            if (string.IsNullOrEmpty(Wcomp_phn1)) WcompRow.SetWcomp_phn1Null(); else WcompRow.Wcomp_phn1 = Wcomp_phn1;
            if (string.IsNullOrEmpty(Wcomp_phn2)) WcompRow.SetWcomp_phn2Null(); else WcompRow.Wcomp_phn2 = Wcomp_phn2;
            if (string.IsNullOrEmpty(Wcomp_fax1)) WcompRow.SetWcomp_fax1Null(); else WcompRow.Wcomp_fax1 = Wcomp_fax1;
            if (string.IsNullOrEmpty(Wcomp_fax2)) WcompRow.SetWcomp_fax2Null(); else WcompRow.Wcomp_fax2 = Wcomp_fax2;
            if (string.IsNullOrEmpty(Wcomp_email2)) WcompRow.SetWcomp_email2Null(); else WcompRow.Wcomp_email2 = Wcomp_email2;
            if (string.IsNullOrEmpty(Wcomp_wsite)) WcompRow.SetWcomp_wsiteNull(); else WcompRow.Wcomp_wsite = Wcomp_wsite;
            if (Wcomp_improtfrmEG == null) WcompRow.SetWcomp_improtfrmEGNull(); else WcompRow.Wcomp_improtfrmEG = Wcomp_improtfrmEG.Value;
            if (CompType_Id == null) WcompRow.SetCompType_IdNull(); else WcompRow.CompType_Id = CompType_Id.Value;
            if (WComp_Sector_id == null) WcompRow.SetWComp_Sector_idNull(); else WcompRow.WComp_Sector_id = WComp_Sector_id.Value;
            if (string.IsNullOrEmpty(WComp_ProductRangeAr)) WcompRow.SetWComp_ProductRangeArNull(); else WcompRow.WComp_ProductRangeAr = WComp_ProductRangeAr;
            if (string.IsNullOrEmpty(WComp_ProductRangeEn)) WcompRow.SetWComp_ProductRangeEnNull(); else WcompRow.WComp_ProductRangeEn = WComp_ProductRangeEn;

            WcompRow.Wcomp_fulnmen = Wcomp_fulnmen;
            WcompRow.Wcomp_smlnmen = Wcomp_smlnmen;
            WcompRow.Wcomp_email1 = Wcomp_email1;
            WcompRow.City_id = City_id;
            WcompRow.Ecs_InternalOffice_ID = Ecs_InternalOffice_ID;

            WcompTable.AddWcompanyRow(WcompRow);
            int rowsAffected = WcompanyAdapter.Update(WcompTable);

            return(rowsAffected == 1) ? WcompRow.Wcomp_id : -1;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Insert, false)]
        public int AddWcompany(string Wcomp_fulnmar,
            string Wcomp_fulnmen,
            string Wcomp_addrs, 
            string Wcomp_phn1, string Wcomp_fax1,            
            string Wcomp_email1, string Wcomp_wsite,
            bool? Wcomp_improtfrmEG, int? CompType_Id,
            int City_id, int Ecs_InternalOffice_ID, 
            bool? Formemberonly, 
            int? WComp_Sector_id, string WComp_ProductRangeAr, string WComp_ProductRangeEn)
        {

            EcsSiteDS.WcompanyDataTable WcompTable = new EcsSiteDS.WcompanyDataTable();
            EcsSiteDS.WcompanyRow WcompRow = WcompTable.NewWcompanyRow();

            if (string.IsNullOrEmpty(Wcomp_fulnmar)) WcompRow.SetWcomp_fulnmarNull(); else WcompRow.Wcomp_fulnmar = Wcomp_fulnmar;
            
            if (string.IsNullOrEmpty(Wcomp_addrs)) WcompRow.SetWcomp_addrsNull(); else WcompRow.Wcomp_addrs = Wcomp_addrs;
            if (string.IsNullOrEmpty(Wcomp_phn1)) WcompRow.SetWcomp_phn1Null(); else WcompRow.Wcomp_phn1 = Wcomp_phn1;
            
            if (string.IsNullOrEmpty(Wcomp_fax1)) WcompRow.SetWcomp_fax1Null(); else WcompRow.Wcomp_fax1 = Wcomp_fax1;
                        
            if (string.IsNullOrEmpty(Wcomp_wsite)) WcompRow.SetWcomp_wsiteNull(); else WcompRow.Wcomp_wsite = Wcomp_wsite;
            if (Wcomp_improtfrmEG == null) WcompRow.SetWcomp_improtfrmEGNull(); else WcompRow.Wcomp_improtfrmEG = Wcomp_improtfrmEG.Value;
            if (CompType_Id == null) WcompRow.SetCompType_IdNull(); else WcompRow.CompType_Id = CompType_Id.Value;
            if (WComp_Sector_id == null) WcompRow.SetWComp_Sector_idNull(); else WcompRow.WComp_Sector_id = WComp_Sector_id.Value;
            if (string.IsNullOrEmpty(WComp_ProductRangeAr)) WcompRow.SetWComp_ProductRangeArNull(); else WcompRow.WComp_ProductRangeAr = WComp_ProductRangeAr;
            if (string.IsNullOrEmpty(WComp_ProductRangeEn)) WcompRow.SetWComp_ProductRangeEnNull(); else WcompRow.WComp_ProductRangeEn = WComp_ProductRangeEn;
            
            WcompRow.Wcomp_fulnmen = Wcomp_fulnmen;
            
            WcompRow.Wcomp_email1 = Wcomp_email1;
            WcompRow.City_id = City_id;
            WcompRow.Ecs_InternalOffice_ID = Ecs_InternalOffice_ID;

            WcompTable.AddWcompanyRow(WcompRow);
            int rowsAffected = WcompanyAdapter.Update(WcompTable);
            return (rowsAffected == 1) ? WcompRow.Wcomp_id : -1;
        }




        #endregion

        #region updateWcompany

        [DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
        public bool updateWcomp(  string Wcomp_fulnmar, string Wcomp_smlnmar, string Wcomp_fulnmen, 
            string Wcomp_smlnmen, string Wcomp_addrs, string Wcomp_desc, string Wcomp_phn1, 
            string Wcomp_phn2, string Wcomp_fax1, string Wcomp_fax2, string Wcomp_mbil1, 
            string Wcomp_mbil2, string Wcomp_email1, string Wcomp_email2, 
            string Wcomp_wsite, bool? Wcomp_improtfrmEG, int? CompType_Id, 
            int City_id, int Ecs_InternalOffice_ID, bool? Formemberonly, int? WComp_Sector_id, 
            string WComp_ProductRangeAr, string WComp_ProductRangeEn, int Wcomp_id)
        {
            EcsSiteDS.WcompanyDataTable WcompTable = WcompanyAdapter.GetWcompanyDataByWcompId(Wcomp_id);
            if (WcompTable.Count == 0)
            {
                return false;
            }
            
            EcsSiteDS.WcompanyRow WcompRow = WcompTable[0];

            if (string.IsNullOrEmpty(Wcomp_fulnmar)) WcompRow.SetWcomp_fulnmarNull(); else WcompRow.Wcomp_fulnmar = Wcomp_fulnmar;
            if (string.IsNullOrEmpty(Wcomp_smlnmar)) WcompRow.SetWcomp_smlnmarNull(); else WcompRow.Wcomp_smlnmar = Wcomp_smlnmar;
            if (string.IsNullOrEmpty(Wcomp_addrs)) WcompRow.SetWcomp_addrsNull(); else WcompRow.Wcomp_addrs = Wcomp_addrs;
            if (string.IsNullOrEmpty(Wcomp_phn1)) WcompRow.SetWcomp_phn1Null(); else WcompRow.Wcomp_phn1 = Wcomp_phn1;
            if (string.IsNullOrEmpty(Wcomp_phn2)) WcompRow.SetWcomp_phn2Null(); else WcompRow.Wcomp_phn2 = Wcomp_phn2;
            if (string.IsNullOrEmpty(Wcomp_fax1)) WcompRow.SetWcomp_fax1Null(); else WcompRow.Wcomp_fax1 = Wcomp_fax1;
            if (string.IsNullOrEmpty(Wcomp_fax2)) WcompRow.SetWcomp_fax2Null(); else WcompRow.Wcomp_fax2 = Wcomp_fax2;
            if (string.IsNullOrEmpty(Wcomp_email2)) WcompRow.SetWcomp_email2Null(); else WcompRow.Wcomp_email2 = Wcomp_email2;
            if (string.IsNullOrEmpty(Wcomp_wsite)) WcompRow.SetWcomp_wsiteNull(); else WcompRow.Wcomp_wsite = Wcomp_wsite;
            if (Wcomp_improtfrmEG == null) WcompRow.SetWcomp_improtfrmEGNull(); else WcompRow.Wcomp_improtfrmEG = Wcomp_improtfrmEG.Value;
            if (CompType_Id == null) WcompRow.SetCompType_IdNull(); else WcompRow.CompType_Id = CompType_Id.Value;
            if (WComp_Sector_id == null) WcompRow.SetWComp_Sector_idNull(); else WcompRow.WComp_Sector_id = WComp_Sector_id.Value;
            if (string.IsNullOrEmpty(WComp_ProductRangeAr)) WcompRow.SetWComp_ProductRangeArNull(); else WcompRow.WComp_ProductRangeAr = WComp_ProductRangeAr;
            if (string.IsNullOrEmpty(WComp_ProductRangeEn)) WcompRow.SetWComp_ProductRangeEnNull(); else WcompRow.WComp_ProductRangeEn = WComp_ProductRangeEn;


            WcompRow.Wcomp_fulnmen = Wcomp_fulnmen;
            WcompRow.Wcomp_smlnmen = Wcomp_smlnmen;
            WcompRow.Wcomp_email1 = Wcomp_email1;
            WcompRow.City_id = City_id;
            WcompRow.Ecs_InternalOffice_ID = Ecs_InternalOffice_ID;            
            int roweffected = WcompanyAdapter.Update(WcompRow);
            return roweffected == 1;
        }
        #endregion

        #region DeleteWcompany

        [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
        public bool DeleteWcompany(int Wcomp_id)
        {
            PersonBLL personAPI = new PersonBLL();
            EcsSiteDS.PersonDataTable personTable =personAPI.GetPersonDataByWcompanyID(Wcomp_id);
            if (personTable.Count != 0)
            {
                EcsSiteDS.PersonRow personRow = personTable[0];
                personAPI.DeletePerson(personRow.Persn_id);
            }
            int rowsAffected = WcompanyAdapter.Delete(Wcomp_id);
            return rowsAffected == 1;
        }
        #endregion
    }
}